home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / doc / gpc / demos / dynamicarraydemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-08  |  2.1 KB  |  67 lines

  1. {
  2. GPC demo program about dynamic arrays (Extended Pascal).
  3.  
  4. Copyright (C) 2000-2001 Free Software Foundation, Inc.
  5.  
  6. Author: Frank Heckenbach <frank@pascal.gnu.de>
  7.  
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, version 2.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; see the file COPYING. If not, write to
  19. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.
  21.  
  22. As a special exception, if you incorporate even large parts of the
  23. code of this demo program into another program with substantially
  24. different functionality, this does not cause the other program to
  25. be covered by the GNU General Public License. This exception does
  26. not however invalidate any other reasons why it might be covered
  27. by the GNU General Public License.
  28. }
  29.  
  30. program DynamicArrayDemo;
  31.  
  32. procedure Test (n : Integer);
  33. var
  34.   { A dynamic array. You don't need pointers and `GetMem' to do this
  35.     in GPC. }
  36.   s, c : array [0 .. n - 1] of Real;
  37.   i : Integer;
  38. begin
  39.   Writeln ('Initializing the array with `sin'' and `cos'' values.');
  40.   for i := 0 to n - 1 do
  41.     begin
  42.       s [i] := Sin (2 * Pi * i / n);
  43.       c [i] := Cos (2 * Pi * i / n)
  44.     end;
  45.   Writeln;
  46.   Writeln ('Outputting the values from the array. This could be used for fast');
  47.   Writeln ('lookups when the values are needed often.');
  48.   Writeln;
  49.   Writeln ('i' : 10, 'sin (2 pi i / n)' : 20, 'cos (2 pi i / n)' : 20);
  50.   for i := 0 to n - 1 do
  51.     Writeln (i : 10, s [i] : 20 : 15, c [i] : 20 : 15)
  52.   { At the end of the procedure the memory of the array is
  53.     automatically freed, just like it is for variables of constant
  54.     size. }
  55. end;
  56.  
  57. var
  58.   n : Integer;
  59.  
  60. begin
  61.   repeat
  62.     Write ('Enter the size for an array: ');
  63.     Readln (n)
  64.   until n > 0;
  65.   Test (n)
  66. end.
  67.